package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IDictionaryService;
import cn.sxt.clinic.service.IMedicineInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/medicineInfo")
public class MedicineInfoController {

    @Autowired
    private IMedicineInfoService medicineInfoService;

    @Autowired
    private IDictionaryService dictionaryService;

    @RequiresPermissions("medicineInfo:medicineInfoManager")
    @RequestMapping("/medicineInfoManager")
    public String medicineInfoManager(){
        return "medicineInfoManager";
    }

    @RequiresPermissions("medicineInfo:list")
    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             String keyword){

        //分页对象
        Page<MedicineInfo> page = new Page<>(pageNum, pageSize);
        //条件对象
        QueryWrapper<MedicineInfo> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)){
            //根据挂号类型模糊查询
            queryWrapper.like("medicine_name",keyword);

            queryWrapper.or();
            queryWrapper.like("dictionary_type",keyword);
        }

        //查询结果
        Page<MedicineInfo> result = medicineInfoService.page(page,queryWrapper);
        return new ResultObject(result);
    }

    @RequiresPermissions("medicineInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public MessageObject delete(long medicineInfoId){

        boolean flag = medicineInfoService.removeById(medicineInfoId);

        MessageObject mo = new MessageObject(1, "删除数据成功");
        if (!flag){
            mo = new MessageObject(0, "删除数据失败,请联系管理员");
        }
        return mo;
    }

    @RequestMapping("/medicineInfoEdit")
    public String medicineInfoEdit(Model m, Long medicineInfoId){
        //如果id为空，添加，为空，修改
        if (medicineInfoId != null){
            MedicineInfo medicineInfo = medicineInfoService.getById(medicineInfoId);
            m.addAttribute("medicineInfo",medicineInfo);
        }

        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",46);

        List<Dictionary> medicineType = dictionaryService.list(wrapper);

        m.addAttribute("medicineType",medicineType);

        QueryWrapper<Dictionary> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id",38);

        List<Dictionary> mincineUint = dictionaryService.list(wrapper1);

        m.addAttribute("mincineUint",mincineUint);

        QueryWrapper<Dictionary> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("parent_id",3);

        List<Dictionary> medicineSpecs = dictionaryService.list(wrapper2);

        m.addAttribute("medicineSpecs",medicineSpecs);

        return "medicineInfoEdit";
    }

    @RequiresPermissions("medicineInfo:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public MessageObject insert(MedicineInfo medicineInfo) {

        medicineInfo.setCreateDate(LocalDate.now());

        boolean flag = medicineInfoService.saveOrUpdate(medicineInfo);

        MessageObject mo = new MessageObject(1, "新增数据成功");
        if (!flag) {
            mo = new MessageObject(0, "新增数据失败,请联系管理员");
        }
        return mo;
    }

    @RequiresPermissions("medicineInfo:update")
    @RequestMapping("/update")
    @ResponseBody
    public MessageObject update(MedicineInfo medicineInfo) {

        boolean flag = medicineInfoService.saveOrUpdate(medicineInfo);

        MessageObject mo = new MessageObject(1, "修改数据成功");
        if (!flag) {
            mo = new MessageObject(0, "修改数据失败,请联系管理员");
        }
        return mo;
    }

}

